var main=[[ //表头
	{type:'checkbox', fixed: 'left'},
	{field: 'audit_no', title: '购车申请单号',  sort: true, fixed: 'left'}
    ,{field: 'brand', title: '品牌',  sort: true}
    ,{field: 'car_type_name', title: '车型'} 
    ,{field: 'company_name', title: '公司'}
    ,{field: 'date', title: '申请时间'}
    ,{field:'state2', title:'状态'}
   	,{ title: '操作',width:250, toolbar:'#operation',fixed: 'right'}
]];
var fi=1;
var page=1;
var data = {};
data.page = page;
data.pagenum = 10;
var where_json = {};
	{if $Think.get.car_audit_id}
		where_json.car_audit_id=['like',"%{$Think.get.car_audit_id}%"];
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like',"%{$Think.get.company_name}%"];
	{/if}
	{if $Think.get.number}
		where_json.number=['like',"%{$Think.get.number}%"];
	{/if}
	{if $Think.get.state}
		where_json.state=['in',"{$Think.get.state}"];
	{/if}
	{if $Think.get.is_board}
		where_json.state=['>=',"5"];
	{/if}
	data.where_json = JSON.stringify(where_json)
	ajaxbuycar(data)
	
	$('.add').on('click',function(){
		window.location.href='{:url("html/car/auditadd")}'
	})
	function ajaxbuycar(data) {
		Load()
	ajax(api.cycle_buy.list, data, 'post').done(function(res) {
		console.log(res)
		Load(1)
		if(res.code == 1) {
			for(var stateidx = 0; stateidx < res.data.length; stateidx++) {
				if(res.data[stateidx].state == 1) {
					res.data[stateidx].state2 = '待审核'
				} else if(res.data[stateidx].state == 2) {
					res.data[stateidx].state2 = '已审核'
				} else if(res.data[stateidx].state == 3) {
					res.data[stateidx].state2 = '审核不通过'
				} else if(res.data[stateidx].state == 4) {
					res.data[stateidx].state2 = '已登记'
				} else if(res.data[stateidx].state == 5) {
					{if $Think.get.is_board}
					res.data[stateidx].state2 = '未上牌'
					{else/}
					res.data[stateidx].state2 = '已审批'
					{/if}
				}else if(res.data[stateidx].state == 6) {
					{if $Think.get.is_board}
					res.data[stateidx].state2 = '已上牌'
					{else/}
					res.data[stateidx].state2 = '已审批'
					{/if}
				}
			}
			table.render({
    				elem: '#buy',
    				cellMinWidth: 80,
    				cols: main,
    				data: res.data,
    				limit: data.pagenum,
    				even: true
    			});
    			if (fi == 1) {
    				laypage.render({
    					elem: 'page',
    					count: res.count,
						layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
						theme: '#1E9FFF',
    					jump: function (obj, first) {
    						if (!first) {
    							page = obj.curr
    							data.page = page
    							pagenum = obj.limit
    							data.pagenum = pagenum
    							ajaxbuycar(data)
    						} else {

    						}
    					}
    				});
    				fi = 2
    			}
    		table.on('tool(buy)', function(obj) {
				var data1 = obj.data; //获得当前行数据
				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				var tr = obj.tr; //获得当前行 tr 的DOM对象
				if(layEvent === 'detail') {
					auditdetail(data1)
				}else if(layEvent === 'check'){
					propcheck(data1)
				}else if(layEvent === 'register'){
//					console.log(data1)
					window.location.href = path.car.auditregister+'?audit_id='+data1.car_audit_id
				}else if(layEvent === 'carcheck'){
					window.location.href = path.car.detail+'?car_id='+data1.car_id+"&audit="+data1.car_audit_id
				}else if(layEvent === 'board'){
					board(data1)
				}
				
			})
    		
			
			
			
		} else {
			Err('请求失败')
		}
	}).fail(

	)
}


	//申请审核
	function propcheck(obj){
		
		var auto = layer.open({
   		type: 1,
   		title:'申请审核',
   		skin: 'layui-layer-rim', //加上边框
   		area: ['300px', '400px'], //宽高
   		content: `
   			<div class='layui-row' style='font-size:12px;padding:10px'>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					购车单位: ${obj.company_name}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					品牌:${obj.brand}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					承载人数:${obj.carry}
   				</div>
   				
   				<div class='layui-col-sm12 po_relative' style='margin-bottom:10px'>
   					<div class='am-fl buy_money'>购车预算:<span>${obj.budget}</span></div>
   					<div class='po_absolute' style='top:0;left:120px'>
   						<button class='layui-btn layui-btn-xs cg_money' >修改预算金额</button>
   						<div class='layui-col-sm12 cg_input_w' style='display:none ;z-index:9999'>
	   						<input type='number' class='cg_input' placeholder='请输入修改金额' style='width:100px;height:20px' value='${obj.budget}'/>
	   						<div style='padding:5px'>
		   						<button class='layui-btn layui-btn-xs cg_input_ok' >确定</button>
		   						<button class='layui-btn layui-btn-xs cg_input_no' >取消</button>
	   						</div>
   						</div>
   					</div>
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					购车用途:${obj.purpose}
   				</div>
   				
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					购车理由:${obj.remark}
   				</div>
   				<div class='layui-col-sm12' style='text-align:center'>
   					<a class='layui-btn layui-btn-sm apply_ok'>同意</a>
   					<a class='layui-btn layui-btn-sm apply_no'>拒绝</a>
   				</div>
   			</div>
   		`,
   		success:function(){
   			//同意审核
   			$('.apply_ok').on('click',function(){
   				var budget=obj.budget
   				
   				console.log(obj.car_audit_id)
   				ajax(api.cycle_buy.check,{
   						car_audit_id:obj.car_audit_id,
   						money:$('.buy_money span').html(),
   						state:2
   					},'post').done((res)=>{
   						if(res.code==1){
   							Msg('审核通过')
   							ajaxbuycar(data)
   						}else{
   							Err(res.msg)
   							
   						}
   						layer.close(auto)
   					}).fail(()=>{
   						Err('请求失败')
   						layer.close(auto)
   					})
   			})
   			//审核不通过
   			$('.apply_no').on('click',function(){
   				layer.close(auto)
   			})
   			//修改预算金额
   			$('.cg_money').on('click',function(){
   				$(this).css('display','none')
   				$('.cg_input_w').css('display','block')
   			})
   			$('.cg_input_ok').on('click',function(){
   				$('.buy_money').html('购车预算:<span>'+$('.cg_input').val()+'</sapn>')
   				$('.cg_money').css('display','block')
   				$(this).closest('.cg_input_w').css('display','none')
   			})
   			$('.cg_input_no').on('click',function(){
   				$('.cg_money').css('display','block')
   				$(this).closest('.cg_input_w').css('display','none')
   			})
   		}
   		})
	}
	//查看详情
	function auditdetail(obj){
		var detailmain='';
		var lay_height=300;
		detailmain=`<div class='layui-col-sm12' style='font-size:12px;padding:10px'>
   				
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					购车单位: ${obj.company_name}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					品牌: ${obj.brand}
   				</div>`
		if(obj.state==2){
			lay_height="400px"
			detailmain+=`<div class='layui-col-sm12' style='margin-bottom:10px'>
   					申请时间: ${obj.date}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					审核时间: ${obj.date_2}
   				</div>`
		}else if(obj.state==3){
			lay_height="450px"
			detailmain+=`
				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					车牌号:<a href='{:url("detail")}'> ${obj.number}</a>
   				</div>
				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					申请时间: ${obj.date}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					审核时间: ${obj.date_2}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					登记时间: ${obj.date_4}
   				</div>	
			`
		}else if(obj.state>=4){
			lay_height="460px"
			detailmain+=`
				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					车牌号: <a href='{:url("detail")}'> ${obj.number?obj.number:'-'}</a>
   				</div>
				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					申请时间: ${obj.date?obj.date:'-'}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					审核时间: ${obj.date_2?obj.date_2:'-'}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					登记时间: ${obj.date_4?obj.date_4:'-'}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					车辆审批时间: ${obj.date_5?obj.date_5:'-'}
   				</div>	
			`
		}
		detailmain+=`<div class='layui-col-sm12' style='margin-bottom:10px'>
   					承载人数: ${obj.carry?obj.carry:'-'}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					申请人: ${obj.proposer?obj.proposer:'-'}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					购车预算: ${obj.budget?obj.budget:'-'}
   				</div>
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					购车用途: ${obj.purpose?obj.purpose:'-'}
   				</div>
   				
   				<div class='layui-col-sm12' style='margin-bottom:10px'>
   					购车理由: ${obj.remark?obj.remark:'-'}
   				</div>
   				<div class='layui-col-sm12' style='text-align:center'>
   					<button class='layui-btn layui-btn-sm detail_close'>取消</a>
   				</div>
   				</div>`
		
		var detail = layer.open({
	   		type: 1,
	   		title:'审核详情',
	   		skin: 'layui-layer-rim', //加上边框
	   		area: ['300px',lay_height], //宽高
	   		content: detailmain,
	   		success:function(){
	   			$('.detail_close').on('click',function(){
	   				layer.close(detail)
	   			})
	   		}
	   		})
	}
	//上牌
	
	function board(main){
		auto = layer.open({
	   		type: 1,
	   		title:false,
	   		skin: 'layui-layer-rim', //加上边框
	   		area: ['300px', '180px'], //宽高
	   		content: `
	   			<div class='layui-col-sm12' style='padding:10px'>
	   				<div class='layui-col-sm12' style='text-align:center;margin-bottom:10px'>
		   				<div class="layui-form-item ">
		   					<input type='text' class='layui-input number' placeholder="请输入车牌号">
		   				</div>
		   				<div class="layui-form-item ">
		   					<input type='text' id='board_time' class='layui-input' placeholder="请选择上牌日期">
		   				</div>
	   				</div>
	   				<div class='layui-col-sm12' style='text-align:center'>
	   					<a class='layui-btn layui-btn-sm layui-btn-normal board_ok'>确定</a>
	   					<a class='layui-btn layui-btn-sm layui-btn-normal board_no'>取消</a>
	   				</div>
	   			</div>
	   		`,
	   		success:function(){
	   			laydate.render({
					  elem: '#board_time'
					});
	   			$('.board_ok').on('click',function(){
	   				Load()
	   				ajax(api.cycle_buy.board,{
   						car_id:main.car_id,
   						car_number:$('.number').val()
   					},'post').done((res)=>{
   						Load(1)
   						if(res.code==1){
   							Msg('上牌完成')
   							ajaxbuycar(data)
   						}else{
   							Err(res.msg)
   							
   						}
   						layer.close(auto)
   					}).fail(()=>{
   						Load(1)
   						Err('请求失败')
   						layer.close(auto)
   					})
	   			});
	   			$('.close_no').on('click',function(){
	   				layer.close(auto)
	   			})
	   		}
	   	})
	
	}
